﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using Microsoft.SharePoint.WebPartPages;
using uk.ac.soton.ses.DAL;
using System.Web;
using System.Text.RegularExpressions;
using System.Linq;
using System.Web.UI.HtmlControls;

namespace uk.ac.soton.ses
{
    public partial class CtDataViewerUserControl : UserControl
    {
        //public HiddenField hidden_ExperimentDataFileID { get { return experimentDataFileID; } }

        private int _dataFileID = -1;
        public int DataFileID { get { return this._dataFileID; } set { this._dataFileID = value; } }
        MaterialsMetadataContext _dbcontext;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this._dbcontext = HttpContext.Current.GetMaterialsDBContext();
            this.Page.AddHDCStylesheet();
            this.Page.AddHDCJQuery(true);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (this._dataFileID == -1)
            {
                //If we still don't have a data file ID, just give up
                return;
            }
            else
            {
                this.DataBindControls();
            }


        }

        private void DataBindControls()
        {
            //Set the data file ID so the client script can access it
            this.experimentDataFileID.Value = this._dataFileID.ToString();

            ExperimentDataFile datafile = this._dbcontext.GetExperimentDataFileByID(this._dataFileID);
            if (!datafile.ExperimentReference.IsLoaded)
            {
                datafile.ExperimentReference.Load();
            }
            if (datafile != null)
            {
                this.Label_ID.Text = datafile.ID.ToString();
                this.Label_DataFileName.Text = datafile.Filename;

                if (string.IsNullOrEmpty(datafile.Description))
                {
                    this.Label_DescriptionTitle.Text = "No description";
                    this.Label_Description.Text = "";
                    this.Label_Description.Visible = false;
                }
                else
                {
                    this.Label_DescriptionTitle.Text = "With description";
                    this.Label_Description.Text = datafile.Description;
                    this.Label_Description.Visible = true;
                }

                this.Label_LocationTitle.Text = "File stored in";
                this.Label_Location.Text = Path.GetDirectoryName(datafile.GetUNCPath(this._dbcontext)) ?? datafile.GetFullPath(); ;

                //We will use the full path to get the file name, in case the file is nested inside a folder
                string filename = Path.GetFileName(datafile.GetFullPath());
                int x_size;
                int y_size;
                int z_size;
                try
                {
                    this.GetSizesFromFilename(filename, out x_size, out y_size, out z_size);
                    this.txt_xsize.Text = x_size.ToString();
                    this.txt_ysize.Text = y_size.ToString();
                    this.txt_zsize.Text = z_size.ToString();

                    this.Label_xsize.Text = x_size.ToString();
                    this.Label_ysize.Text = y_size.ToString();
                    this.Label_zsize.Text = z_size.ToString();
                }
                catch (NotSupportedException)
                {
                    //This means we couldn't parse the file name so we will not set the X, Y and Z values
                }
            }
        }

        private void GetSizesFromFilename(string filename, out int x, out int y, out int z)
        {
            string dimensions = Regex.Match(filename, @"(\d+[_\-x]\d+[_\-x]\d+)").Value;
            string dimensions_csv = Regex.Replace(dimensions, @"[_\-x]", ",");
            string[] xyz = dimensions_csv.Split(',');
            if (xyz.Count() == 3)
            {
                x = Convert.ToInt32(xyz[0]);
                y = Convert.ToInt32(xyz[1]);
                z = Convert.ToInt32(xyz[2]);
            }
            else
            {
                throw new NotSupportedException("Filename could not be parsed");
            }
        }
        
    }
}
